<?php
namespace app\admin\controller;

use app\admin\model\slideCat as sc;
use think\Db;
use my\Front;
use my\Page;
use my\Html;
use think\Input;
use org\Upload;

class Slide extends Base
{   
    public function lists(){
        //ajax查询数据
        if( is_post() ){
            $page     = input('page');
            $cat_id   = input('cat_id');
            $status   = input('status');
            $keyword  = input('keyword');
            
            //原生态sql查询
            $sql = "
                select
                    [1]
                from
                    slide a
                left join
                    slide_cat ac
                on
                    a.cat_id=ac.id
                where 
                    1 = ?
                ";
            $param = [1];
            if ( $cat_id != '100all' ) {
                $sql .= " and a.cat_id = ?";
                $param[] = $cat_id;
            }
            if ( $status != '100all' ) {
                $sql .= " and a.status = ?";
                $param[] = $status;
            }
            if ( !empty($keyword) ) {
                $sql .= " and a.title like ?";
                $param[] = "%{$keyword}%";
            }
            //总条数
            $sql_count = str_replace("[1]", "count(*) as total", $sql);
            $total     = Db::query($sql_count, $param);
            $total     = $total[0]['total'];
            
            //分页数据
            $page_size   = c('page_size');     #分页条数
            $page        = com_cur_page($total, $page,  $page_size);   #当前页
            $page_data   = com_page_data($total, $page,  $page_size);   #分页原始数据
            
            //列表数据
            $start       = ($page - 1) * $page_size;
            $sql        .= " order by a.sort desc, a.id desc limit $start, $page_size";
            $field       = "a.*, ac.cat_title";
            $sql_list    = str_replace("[1]", $field, $sql);
            $list        = Db::query($sql_list, $param);

            //格式数据
            foreach($list as $k => $v){
                $list[$k]['checkbox']   =  "<input type='checkbox' name='id[]' tid='{$v['id']}'>";
                $list[$k]['create_time']    =   $v['create_time'] ? date("Y-m-d H:i",$v['create_time']) : "";
                $list[$k]['url']    =   $v['url'] ? "<a href='{$v['url']}' target='_blank'>{$v['url']}</a>" : "";
                //图片
                $list[$k]['pic']    =   com_format_pic(com_get_pic($v['pic'],'small'));
                //排序
                $list[$k]['sort']       =   "<input style='width:50px;' type='text' _url='".url('slide/editSort')."' check='number' tid='{$v['id']}' old='{$v['sort']}' value='{$v['sort']}' onchange='com_edit(this)' />";
                
                //连接类型
                $selected0 = $v['link_type']==0 ? "selected=true" : "";
                $selected1 = $v['link_type']==1 ? "selected=true" : "";
                $list[$k]['link_type']     =   "<select style='width:80px;' _url='".url('slide/editLinkType')."' tid='{$v['id']}' onchange='com_edit(this)'>";
                $list[$k]['link_type']    .=   "<option value='0' ".$selected0.">当前窗口</option>";
                $list[$k]['link_type']    .=   "<option value='1' ".$selected1.">新窗口</option>";
                $list[$k]['link_type']    .=   "</select>";
                
                //状态
                $selected0 = $v['status']==0 ? "selected=true" : "";
                $selected1 = $v['status']==1 ? "selected=true" : "";
                $list[$k]['status']     =   "<select style='width:50px;' _url='".url('slide/editStatus')."' tid='{$v['id']}' onchange='com_edit(this)'>";
                $list[$k]['status']    .=   "<option value='0' ".$selected0.">开启</option>";
                $list[$k]['status']    .=   "<option value='1' ".$selected1.">关闭</option>";
                $list[$k]['status']    .=   "</select>";
                
                
                //操作按钮
                $list[$k]['action']    =   "<a class='ml-5' tid='{$v['id']}' _url='".url("slide/edit","id={$v['id']}")."' msg='编辑' onclick='com_page(this)' href='javascript:;' title='编辑'><i class='Hui-iconfont'>&#xe6df;</i></a>";
                $list[$k]['action']   .=   "<a class='ml-5' tid='{$v['id']}' _url='".url('slide/delete')."' msg='你确定要删除[{$v['title']}]吗?' onclick='com_del(this)' href='javascript:;' title='删除'><i class='Hui-iconfont'>&#xe6e2;</i></a>";
            }
            $com_body    = Html::buildTable($list, array("checkbox", "id",  "title","pic", "cat_title","url", "link_type","sort","status","create_time", "action"));
            $com_page    = Html::buildPage( $page_data );
            $data = array(
                "com_body"  => $com_body,
                "com_page"  => $com_page,
            );
            Front::check(['data'=>$data]);
        }
       	
       	//模板赋值
        $all = [[
            'id'            => '100all',
            'cat_title'     => '全部分类',
        ]];
        $category = array_merge($all , Db::name('slide_cat')->select());
        $data = [
            'category'  => $category,
            'status'    => ['100all'=>'全部状态'] + c('type.status'),
            'nav'       => ['幻灯管理', '列表'],
        ];
        return view('', $data);
    }

    //编辑状态
    public function editStatus(){
        if( is_post() ){
            $data   = [
                'id'        => input('id'),
                'status'    => (int)input('val'),
            ];
            Db::name('slide')->update( $data );
            $config = [
                'msg' => '编辑状态成功！',
                'suc' => [
                    'js_action' => 'alert',
                ],
            ];
            Front::check( $config );
        }
    }
    //编辑排序
    public function editSort(){
        if( is_post() ){
            $data   = [
                'id'    => input('id'),
                'sort'  => (int)input('val'),
            ];
            Db::name('slide')->update($data);
            $config = [
                'msg' => '编辑排序成功！',
                'suc' => [
                    'js_action' => 'alert',
                ],
            ];
            Front::check($config);
        }
    }
    //编辑打开方式
    public function editLinkType(){
        if( is_post() ){
            $data   = [
                'id'        => input('id'),
                'link_type' => (int)input('val'),
            ];
            Db::name('slide')->update( $data );
            $config = [
                'msg' => '编辑打开方式成功！',
                'suc' => [
                    'js_action' => 'alert',
                ],
            ];
            Front::check( $config );
        }
    }
    //添加
    public function add(){
        if( is_post() ){
            $data = input();  #post数据
            unset($data['pic']);
            //验证
            $rule = [
                    'title'         =>  'require',
                    'url'           =>  'require',
                    'cat_id'        =>  'number',
                ];
            $msg = [
                   'title.require'      =>  '标题不能为空',
                   'url.require'        =>  '连接地址不能为空',
                   'cat_id.number'      =>  '请选择分类',
                ];
            com_validate( $rule, $msg, $data, true );
            
            if( isset($_FILES['pic']) ){
                $pic = com_up_pic('slide', 'pic', true);
                com_check( ['err'=>['js_action'=>'alert'], 'suc'=>['return_type'=>'return']]);
                $data['pic'] = $pic;
            }
            
            $data['create_time'] = time();
            Db::name('slide')->insert($data);
            $config = [
                'msg' => '添加成功！',
                'suc' => [
                    'js_action' => 'alert|close',
                ],
            ];
            front::check( $config );
        }    
        //模板赋值
        $all = [[
            'id'            => '100all',
            'cat_title'     => '全部分类',
        ]];
        $category = array_merge($all , Db::name('slide_cat')->select());
        $data = [
            'category'  => $category,
            'status'    => c('type.status'),
            'link_type' => c('type.link_type'),
        ];
        return view('', $data);
    }
    
    
    //编辑
    public function edit( $id ){
        if( is_post() ){
            $data = input();  #post数据
            unset($data['pic']);
            //验证
            $rule = [
                    'title'         =>  'require',
                    'url'           =>  'require',
                    'cat_id'        =>  'number',
                ];
            $msg = [
                   'title.require'      =>  '标题不能为空',
                   'url.require'        =>  '连接地址不能为空',
                   'cat_id.number'      =>  '请选择分类',
                ];
            com_validate( $rule, $msg, $data, true );
            
            //pic
            if( isset($_FILES['pic']) ){
                $pic = com_up_pic('article', 'pic', true);
                $data['pic'] = $pic;
                com_check(['err'=>['js_action'=>'alert'], 'suc'=>['return_type'=>'return']] );
            }

            if( isset($data['delete_pic']) ){
                unset($data['delete_pic']);
                if(!isset($_FILES['pic'])) $data['pic'] = '';

            }

            $data['update_time'] = time();
            $data['id'] = $id;
            Db::name('slide')->update($data);
            $config = [
                'suc' => [
                    'msg' => '编辑成功！',
                    'js_action' => 'alert|close',
                ],
            ];
            front::check( $config );
        }
        //模板赋值
        $all = [[
            'id'            => '100all',
            'cat_title'     => '全部分类',
        ]];
        $category = array_merge($all , Db::name('slide_cat')->select());
        $info   = Db::name('slide')->find($id);
        $data = [
            'category'  => $category,
            'status'    => c('type.status'),
            'link_type' => c('type.link_type'),
            'info'      => $info,
        ];
        return view('', $data);
    }
    
    //删除
    public function delete(){
        if( is_post() ){
            Db::name('slide')->delete( input('id') );
            $config = [
                'suc' => [
                    'msg' => '删除成功！',
                    'js_action'=>"alert|reload",
                ],
            ];
            Front::check( $config );
        }
    }
    
    //批量删除
    public function delall(){
        if( is_post() ){
            $ids = input('ids/a');
            Db::name('slide')->delete( $ids );
            $config = [
                'suc' => [
                    'msg' => '删除成功！',
                    'js_action'=>"alert|reload",
                ],
            ];
            Front::check($config);
        }
    }

}